
// Copyright (c) 2009 Mohannad Alharthi (mohannad.harthi@gmail.com)
// All rights reserved.
// This source code is licensed under the BSD license, which can be found in
// the LICENSE.txt file.

#include <cstdarg>

#include "misc.h"

void findAndReplaceAll(std::string& source,
                       const char* old_value,
                       const char* new_value) {
  size_t old_len = strlen(old_value);
  size_t new_len = strlen(new_value);
  size_t position = 0;

  while ((position = source.find(old_value, position)) != std::string::npos) {
    source.replace(position, old_len, new_value);
    position += new_len;
  }
}

std::string format(std::string format_str, ...) {
  va_list arg_list;
  va_start(arg_list, format_str);

  const int buffer_len = 256;
  char buffer[buffer_len];
  vsprintf_s(buffer, buffer_len, format_str.c_str(), arg_list);

  va_end(arg_list);
  return std::string(buffer);
}
